package com.user.service;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;

import javax.transaction.Transactional;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.domain.Specification;
import org.springframework.stereotype.Service;

import com.achievement.dao.CourseRepository;
import com.achievement.entity.Course;
import com.common.util.TablePage;
import com.exam.dao.ExamRepository;
import com.exam.entity.Exam;
import com.user.dao.DepartmentRepository;
import com.user.dao.StudentRepository;
import com.user.entity.Department;
import com.user.entity.Student;
import com.user.entity.dto.StudentDTO;

@Service
@Transactional
public class StudentService implements IStudentService {
	@Autowired
	private StudentRepository studentDao;
	@Autowired 
	private DepartmentRepository departmentDao;
	@Autowired
	private ExamRepository examDao;
	@Autowired
	private CourseRepository courseDao;

	public List<StudentDTO> findAll() {
		List<StudentDTO> result = new ArrayList<StudentDTO>();
		List<Student> list = (List<Student>) studentDao.findAll();
		for(Student s:list) {
			String department = "";
			if(s.getDepartment()!=null)department = s.getDepartment().getName();
			result.add(new StudentDTO(s, department));
		}
		return result;
	}
	public boolean delete(Long id) {
		try {
			studentDao.delete(id);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	public boolean saveOrUpdate(StudentDTO dto) {
		if(dto==null)return false;
		try {
			Department department = new Department();
			if(departmentDao.findByName(dto.getDepartment())!=null)department = departmentDao.findByName(dto.getDepartment());
			Student student = new Student(dto, department);
			studentDao.save(student);
			return true;
		} catch (Exception e) {
			e.printStackTrace();
			return false;
		}
	}
	public List<Exam> findExam(Long id) {
		List<Exam> result = new ArrayList<Exam>();
		List<Object> objs = courseDao.findByStuId(id);
		Iterator iter = objs.iterator();
		List<Course> courses = new ArrayList<Course>();
		while(iter.hasNext()) {
			Object[] obj = (Object[]) iter.next();
			Course course = (Course) obj[0];
			result.addAll(examDao.findStuExam(course.getId()));
		}
		return result;
	}
	
	public StudentDTO findLogin(String num, String password) {
		Student student = studentDao.findLogin(num, password);
		System.out.println(student);
		if(student !=null) {
			String department=null;
			if(student.getDepartment()!=null)department = student.getDepartment().getName();
			return new StudentDTO(student, department);
		}
		return null;
	}

	public TablePage<Course> findCourses(Specification<Course> spec, Pageable pageable) {
		Page<Course> page = courseDao.findAll(spec, pageable);
		return TablePage.getTablePage(page);
	}
	@Override
	public TablePage<Student> findPage(Specification<Student> spec, Pageable pageable) {
		// TODO Auto-generated method stub
		Page<Student> page = studentDao.findAll(spec, pageable);
		return TablePage.getTablePage(page);
	}
	@Override
	public boolean hasCourse(Long id) {
		List<Course> list = courseDao.findByStudentId(id);
		if(list.size()>0)
			return true;
		return false;
	}
	@Override
	public void activate(Long id) {
		// TODO Auto-generated method stub
		Student student = studentDao.findOne(id);
		student.setRevoked(false);
		studentDao.save(student);
	}
	@Override
	public void revoked(Long id) {
		// TODO Auto-generated method stub
		Student student = studentDao.findOne(id);
		student.setRevoked(true);
		studentDao.save(student);
	}
	@Override
	public void saveOrUpdate(Student student) {
		// TODO Auto-generated method stub
		studentDao.save(student);
	}
	@Override
	public boolean isNumExist(Student student) {
		// TODO Auto-generated method stub
		Student s = studentDao.findByNum(student.getNum());
		if(null!=s&&student.getId()!=s.getId())return true;
		return false;
	}
	@Override
	public List<Long> getCourseIds(Long id) {
		// TODO Auto-generated method stub
		List<Long> result = new ArrayList<Long>();
		List<Object> objs = courseDao.findByStuId(id);
		Iterator iter = objs.iterator();
		List<Course> courses = new ArrayList<Course>();
		while(iter.hasNext()) {
			Object[] obj = (Object[]) iter.next();
			Course course = (Course) obj[0];
			result.add(course.getId());
		}
		return result;
	}
	@Override
	public List<Course> getCourseList(Long id) {
		// TODO Auto-generated method stub
		if(null!=id) {
			List<Course> result = new ArrayList<Course>();
			List<Object> objs = courseDao.findByStuId(id);
			Iterator iter = objs.iterator();
			List<Course> courses = new ArrayList<Course>();
			while(iter.hasNext()) {
				Object[] obj = (Object[]) iter.next();
				Course course = (Course) obj[0];
				result.add(course);
			}
			return result;
		}
		return null;
	}
	@Override
	public void updatePwd(Long id,String pwd) {
		// TODO Auto-generated method stub
		Student student = studentDao.findOne(id);
		student.setPassword(pwd);
		studentDao.save(student);
	}
	
}
